package org.restoran.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Snisar Bogdan.
 */
public class Utills {
    private Utills() {
    }

    public static <E> Collection<E> collection(Iterable<E> it) {
        if(it == null) throw new NullPointerException("null iterable");
        if(it instanceof Collection) return (Collection<E>) it;

        Iterator<E> iter = it.iterator();
        List<E> l = new LinkedList<>();
        while (iter.hasNext()) {
            l.add(iter.next());
        }
        return l;
    }

    public static String getClassName() {
        try {
            throw new RuntimeException();
        } catch (RuntimeException e) {
            return e.getStackTrace()[1].getClassName();
        }
    }

    public static <T> T as(Object obj, Class<T> clazz) {
        return obj.getClass() == clazz ? clazz.cast(obj) : null;
    }
}
